package com.kin.rich.service;

import java.util.Date;
import java.util.List;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

import com.kin.rich.dao.CommonDAO;
import com.kin.rich.pojo.Keyword;
import com.kin.rich.util.CalendarFunction;

public class SpiderHelper {

	private CommonDAO commonDAO;

	public void setCommonDAO(CommonDAO commonDAO) {
		this.commonDAO = commonDAO;
	}
	
	public static void main(String[] args) {
		ApplicationContext appContext = new FileSystemXmlApplicationContext("/src/applicationContext.xml");
		SpiderHelper spider =  SpiderHelper.getFromApplicationContext(appContext);
		List<Keyword> list = spider.getValidKeyword(1);
		for(int i=0;i<list.size();i++){
			System.out.println(list.get(i).getWordName());
		}
	}
	
	public Keyword getKeywordById(int id){
		return (Keyword)commonDAO.findByQuery("from Keyword where wordId="+id, 0, 0).get(0);
	}
	
	public List<Keyword> getValidKeyword(){
		String today = CalendarFunction.dateToString(new Date());
		//获取区间内的关键词
		String hql = "select keyword from Company company,Project project,Keyword keyword where company.compEnddate>='"+today+"' and project.projEnddate>='"+today+"'"
		+" and keyword.endWordDate>='"+today+"' and keyword.projId=project.projId and project.compId=company.compId";
		return commonDAO.findByQuery(hql, 0, 0);
	}
	
	public List<Keyword> getValidKeyword(int compId){
		String today = CalendarFunction.dateToString(new Date());
		//获取区间内的关键词
		String hql = "select keyword from Company company,Project project,Keyword keyword where company.compEnddate>='"+today+"' and project.projEnddate>='"+today+"'"
		+" and keyword.endWordDate>='"+today+"' and keyword.projId=project.projId and project.compId=company.compId and company.compId="+compId;
		return commonDAO.findByQuery(hql, 0, 0);
	}
	
	public int getSearchTimes(int wordId,String today,String source){
		//System.out.println("gavc");
		String countHql = "select searchTimes from Searchdata where wordId="+wordId+" and (searchDate between'"+today+"' and '"+today+"') and source='"+source+"' and searchTimes>0";
		return commonDAO.count(countHql);
	}
	
	public int checkDataCat(String url,String title,int wordId){
		String hql = "select dataCat from Searchdata where wordId="+wordId+" and dataTitle like '%"+title+"%' order by id desc";
		int cat = commonDAO.count(hql);
		/*if(cat==0){
			hql = "select dataCat from Searchdata where wordId="+wordId+" and linkUrl='"+url+"' order by id desc";
			cat = commonDAO.count(hql);
		}*/
		return cat;
	}
	
	public boolean checkBaiduNewsSaved(String url,int wordId){
		String hql = "select count(*) from Searchdata where linkUrl='"+url+"' and source like 'BaiduNews%' and wordId="+wordId;
		return commonDAO.count(hql)>0 ? true : false;
	}
	
	public static SpiderHelper getFromApplicationContext(ApplicationContext ctx) {
		return (SpiderHelper) ctx.getBean("SpiderHelper");
	}
	
}
